package com.asset.dispoal.business.make.service.commom;

import com.asset.dispoal.business.make.domain.db.mapper.MakeMapper;
import com.asset.dispoal.business.make.dto.MakePageResponse;
import com.asset.dispoal.business.make.dto.MakeViewPageRequest;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

@Component
public class MakeQueryViewPageCommand implements BaseCommand<MakeViewPageRequest, Result<PageVO<MakePageResponse>>> {

    @Autowired
    MakeMapper makeMapper;

    @Override
    public Result<PageVO<MakePageResponse>> execute(MakeViewPageRequest request) {
        //状态判断(待下单、待接单)不加权限字符
        String permission = (String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission");
        request.setPermission(permission);

        Integer count = makeMapper.queryViewCount(request);
        Page<MakeViewPageRequest> page = PagingQuery.getPage(count, request);
        PageVO<MakePageResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<MakePageResponse> bsCases = makeMapper.queryViewPage(page);
            pageVO.setResult(bsCases);
        }
        return Result.ok(pageVO);
    }
}
